package com.banciyuan.bcywebview.biz.main.mineinfo.mywork;

import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.api.SpaceApi;
import com.banciyuan.bcywebview.base.activity.BaseActivity;
import com.banciyuan.bcywebview.base.helper.ActionbarHelper;
import com.banciyuan.bcywebview.base.helper.LoadingHelper;
import com.banciyuan.bcywebview.base.helper.data.UserDataHelper;
import com.banciyuan.bcywebview.utils.http.BcyPair;
import com.banciyuan.bcywebview.utils.http.HttpUtils;
import com.banciyuan.bcywebview.utils.http.OnErrorProxy;
import com.banciyuan.bcywebview.utils.http.StatusCodeUtils;
import com.banciyuan.bcywebview.utils.http.StringRequestParam;
import com.banciyuan.bcywebview.utils.http.VolleyQueue;
import com.banciyuan.bcywebview.utils.intent.gotoUtil;
import com.banciyuan.bcywebview.utils.universalimageloader.core.ImageLoader;
import com.banciyuan.bcywebview.utils.universalimageloader.utils.ImageOptions;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import de.greenrobot.daoexample.model.MyWork;

/**
 * Created by hy on 2016/3/4.
 */
public class MyWorkManagerActivity extends BaseActivity implements View.OnClickListener {
    private View viewDraw;
    private TextView tvDrawWorksNum;
    private TextView tvDrawLikeNum;

    private View viewCos;
    private TextView tvCosWorksNum;
    private TextView tvCosLikeNum;

    private View viewWrite;
    private TextView tvWriteWorksNum;
    private TextView tvWriteLikeNum;

    private View viewSeriesInner;
    private TextView tvSeriesWorksNum;
    private TextView tvSeriesOrderNum;

    private RequestQueue mQueue;
    private ImageLoader imageLoader;
    private List<MyWork> myWorkList;

    private List<FrameLayout> cosImages = new ArrayList<>();
    private List<FrameLayout> drawImages = new ArrayList<>();
    private FrameLayout writeCover;
    private TextView writetitle;
    private LoadingHelper mProgressbarHelper;
    private View viewParent;

    private String cp_id;
    private String dp_id;
    private String wp_id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        viewParent = View.inflate(this, R.layout.myworkmanager_layout, null);
        setContentView(viewParent);
        initArgs();
        initActionbar();
        initProgressbar();
        initUi();
        initAction();
        initData();
    }

    @Override
    protected void initArgs() {
        mQueue = VolleyQueue.getRquest(this);
        imageLoader = ImageLoader.getInstance();
    }

    @Override
    protected void initProgressbar() {
        mProgressbarHelper = new LoadingHelper(viewParent);
        mProgressbarHelper.setActionCallbacks(new LoadingHelper.OnProgressCallbacks() {
            @Override
            public void onActionClick(int id) {
                mProgressbarHelper.onLoading();
                initData();
            }
        });
        mProgressbarHelper.onLoading();
    }

    @Override
    protected void initActionbar() {
        View mActionBarView = findViewById(R.id.base_action_bar);
        ActionbarHelper mActionbarHelper = new ActionbarHelper(this, mActionBarView);
        mActionbarHelper.setTitle(getString(R.string.my_work));
    }

    @Override
    protected void initUi() {
        viewDraw = findViewById(R.id.view_draw_work);
        tvDrawWorksNum = (TextView) findViewById(R.id.tv_draw_worksnum);
        tvDrawLikeNum = (TextView) findViewById(R.id.tv_draw_likenum);
        drawImages.add((FrameLayout) findViewById(R.id.iv_draw_one));
        drawImages.add((FrameLayout) findViewById(R.id.iv_draw_two));
        drawImages.add((FrameLayout) findViewById(R.id.iv_draw_three));
        drawImages.add((FrameLayout) findViewById(R.id.iv_draw_four));

        viewCos = findViewById(R.id.view_cos_work);
        tvCosWorksNum = (TextView) findViewById(R.id.tv_cos_worksnum);
        tvCosLikeNum = (TextView) findViewById(R.id.tv_cos_likenum);
        cosImages.add((FrameLayout) findViewById(R.id.iv_cos_one));
        cosImages.add((FrameLayout) findViewById(R.id.iv_cos_two));
        cosImages.add((FrameLayout) findViewById(R.id.iv_cos_three));
        cosImages.add((FrameLayout) findViewById(R.id.iv_cos_four));

        viewWrite = findViewById(R.id.view_write_work);
        tvWriteWorksNum = (TextView) findViewById(R.id.tv_write_worksnum);
        tvWriteLikeNum = (TextView) findViewById(R.id.tv_write_likenum);
        writeCover= (FrameLayout) findViewById(R.id.iv_write_one);
        writetitle= (TextView) findViewById(R.id.write_titles);

        viewSeriesInner = findViewById(R.id.view_series_inner);
        tvSeriesWorksNum = (TextView) findViewById(R.id.tv_series_worksnum);
        tvSeriesOrderNum = (TextView) findViewById(R.id.tv_series_ordernum);

        viewCos.setVisibility(View.GONE);
        viewDraw.setVisibility(View.GONE);
        viewWrite.setVisibility(View.GONE);
        viewSeriesInner.setVisibility(View.GONE);
    }

    @Override
    protected void initAction() {
        viewCos.setOnClickListener(this);
        viewDraw.setOnClickListener(this);
        viewSeriesInner.setOnClickListener(this);
        findViewById(R.id.view_write_inner).setOnClickListener(this);
        findViewById(R.id.view_drawer_statistics).setOnClickListener(this);
        findViewById(R.id.view_writer_statistics).setOnClickListener(this);
        findViewById(R.id.view_coser_statistics).setOnClickListener(this);
        findViewById(R.id.achievement_line).setOnClickListener(this);
    }

    @Override
    protected void initData() {
        List<BcyPair> paramsList = new ArrayList<BcyPair>();
        paramsList.add(new BcyPair(HttpUtils.TOKEN, UserDataHelper.getInstance(this).getToken()));
        final HashMap<String, String> params = HttpUtils.getData(paramsList);
        String url = HttpUtils.BASE_URL + SpaceApi.getPostAdmin();
        Response.Listener succ = new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                try {
                    JSONObject result = new JSONObject(s);
                    if (StatusCodeUtils.dealStatusCode(s, MyWorkManagerActivity.this)) {
                        Gson gson = new Gson();
                        myWorkList = gson.fromJson(result.getString(HttpUtils.UPLOAD_DATA),
                                new TypeToken<List<MyWork>>() {
                                }.getType());
                        processData();
                    } else {
                        mProgressbarHelper.onFailed();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    mProgressbarHelper.onFailed();
                }
            }
        };

        Response.ErrorListener error = new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                mProgressbarHelper.onFailed();
            }
        };
        OnErrorProxy errorProxy = new OnErrorProxy(error, succ, url, this, params);
        StringRequestParam stringRequest = new StringRequestParam(Request.Method.POST, url, params, succ, errorProxy);
        mQueue.add(stringRequest);
    }

    private void processData() {
        if (null == myWorkList || myWorkList.size() == 0) {
            mProgressbarHelper.onNodata("", true);
            return;
        }
        int i = 0;
        int count = 0;
        for (; i < myWorkList.size(); i++) {
            MyWork myWork = myWorkList.get(i);
            if ("cos".equals(myWork.getType())) {
                cp_id = myWork.getCp_id();
                viewCos.setVisibility(View.VISIBLE);
                int maxSize = myWork.getNew_posts().size() <= 4 ? myWork.getNew_posts().size() : 4;
                for (int k = 0; k <cosImages.size() ; k++) {
                    if(maxSize-1==k){
                        cosImages.get(k).setVisibility(View.VISIBLE);
                        for (int j = 0; j < maxSize; j++) {
                            imageLoader.displayImage(myWork.getNew_posts().get(j).getImg_src(), (ImageView) cosImages.get(k).getChildAt(j), ImageOptions.options);
                        }
                    }else{
                        cosImages.get(k).setVisibility(View.GONE);
                    }
                }
                tvCosWorksNum.setText(myWork.getPost_count());
                tvCosLikeNum.setText(myWork.getDing_num());
            }
            if ("illust".equals(myWork.getType())) {
                dp_id = myWork.getDp_id();
                viewDraw.setVisibility(View.VISIBLE);
                int maxSize = myWork.getNew_posts().size() <= 4 ? myWork.getNew_posts().size() : 4;
                for (int k = 0; k <cosImages.size() ; k++) {
                    if(maxSize-1==k){
                        cosImages.get(k).setVisibility(View.VISIBLE);
                        for (int j = 0; j < maxSize; j++) {
                            imageLoader.displayImage(myWork.getNew_posts().get(j).getImg_src(), (ImageView) drawImages.get(k).getChildAt(j), ImageOptions.options);
                        }
                    }else{
                        cosImages.get(k).setVisibility(View.GONE);
                    }
                }
                tvDrawWorksNum.setText(myWork.getPost_count());
                tvDrawLikeNum.setText(myWork.getDing_num());
            }
            if ("novel".equals(myWork.getType())) {
                wp_id = myWork.getWp_id();
                if (!"0".equals(myWork.getPost_count())) {
                    imageLoader.displayImage(myWork.getNew_posts().get(0).getCover(), (ImageView) writeCover.getChildAt(0), ImageOptions.options);
                    tvWriteWorksNum.setText(myWork.getPost_count());
                    tvWriteLikeNum.setText(myWork.getDing_num());
                    viewWrite.setVisibility(View.VISIBLE);
                    String result="";
                    for (int j = 0; j <myWork.getNew_posts().size() ; j++) {
                        result+=myWork.getNew_posts().get(j).getTitle()+"\n";
                    }
                    writetitle.setText(result);
                } else {
                    count++;
                }
                if (null != myWork.getNovel_set() && !"0".equals(myWork.getNovel_set().getSet_num())) {
                    viewSeriesInner.setVisibility(View.VISIBLE);
                    tvSeriesWorksNum.setText(myWork.getNovel_set().getSet_num());
                    tvSeriesOrderNum.setText(myWork.getNovel_set().getSubscribed_num());
                }
            }
        }
        if (count == i) {
            mProgressbarHelper.onNodata("", true);
        } else {
            mProgressbarHelper.onSuccess();
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.view_cos_work:
                gotoUtil.gotoAct(this, MainWorkActivity.class, "cos", cp_id);
                break;
            case R.id.view_draw_work:
                gotoUtil.gotoAct(this, MainWorkActivity.class, "illust", dp_id);
                break;
            case R.id.view_write_inner:
                gotoUtil.gotoAct(this, WriteWorkActivity.class, wp_id);
                break;
            case R.id.view_series_inner:
                gotoUtil.gotoAct(this, MyNovelSetActivity.class, wp_id);
                break;
            case R.id.view_drawer_statistics:
                gotoUtil.gotoAct(this, StatisticsActivity.class, 0, dp_id);
                break;
            case R.id.view_writer_statistics:
                gotoUtil.gotoAct(this, StatisticsActivity.class, 1, wp_id);
                break;
            case R.id.view_coser_statistics:
                gotoUtil.gotoAct(this, StatisticsActivity.class, 2, cp_id);
                break;
            case R.id.achievement_line:
                gotoUtil.gotoAct(this, AchievementActivity.class);
                break;
        }
    }
}
